package cn.db101.dao;

import cn.db101.model.admin;
import cn.db101.model.reader;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;


/**
 * ����ԱDao��
 * @author ASUS
 *
 */
public class adminDao {
	private static final Date Date = null;
	/**
	 * ��¼��֤
	 * @param con
	 * @param adm
	 * @return
	 * @throws Exception
	 */
	public static admin login(Connection con, admin adm)throws Exception{
		 admin resultadm=null;
		String sql="select * from admin where account=? and password=?";
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setString(1, adm.getAccount());
		pstmt.setString(2, adm.getPassword());
		ResultSet rs=pstmt.executeQuery();
		if(rs.next()) {
			resultadm=new admin();
			resultadm.setAdmin_no(rs.getString("admin_no"));
			resultadm.setAccount(rs.getString("account"));
			resultadm.setPassword(rs.getString("password"));
		}
		return resultadm;
	}
	/**
	 * �޸�����
	 * @param con
	 * @param stu
	 * @return
	 * @throws Exception
	 */
	public static int alterpassword(Connection con,admin adm)throws Exception{
		String sql="update admin set password=? where account=?";
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setString(2,adm.getAccount());
		pstmt.setString(1,adm.getPassword());
		return pstmt.executeUpdate();
	}
	/**
	 * �޸��˻�
	 * @param con
	 * @param adm
	 * @return
	 * @throws Exception
	 */
	public static int alteraccount(Connection con,admin adm)throws Exception{
		String sql="update admin set admin_name=?,gender=? where account=?";
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setString(3,adm.getAccount());
		pstmt.setString(2,adm.getGender());
		pstmt.setString(1,adm.getAdmin_name());
		return pstmt.executeUpdate();
	}
	/**
	 * ����Ȩ��
	 * @param con
	 * @param rea
	 * @return
	 * @throws Exception
	 */
	public static int limit(Connection con, reader rea)throws Exception{
		java.util.Date  date=new java.util.Date();
		java.sql.Date  register_time=new Date(date.getTime());
		int account_type=0;
		int del=0;
		String sql="insert into admin values(?,?,?,?,?,?,?,?)";
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setString(1,rea.getReader_no());
		pstmt.setString(2,rea.getReader_name());
		pstmt.setString(3,rea.getGender());
		pstmt.setString(4,rea.getReader_no());
		pstmt.setString(5,rea.getPassword());
		pstmt.setDate(6,register_time);
		pstmt.setInt(7,account_type);
		pstmt.setInt(8,del);
		return pstmt.executeUpdate();
	}
}
